/**
 * 进入玩法房间
 */
var ClubBoard = Board.extend({
    _ccsFileName: res.ClubBoard_json,

    initBoard: function (joinClubId) {
        hideLoading();
        if (joinClubId) {
            this._inviteJoinClub(joinClubId);
        } else {
            ClubViewUtil.showClubMainWindow(gameData.roomClubId);
            
        }
        gameData.roomClubId = null;

        // playMusic('bgMusic');
    },

    onEnterTransitionDidFinish: function () {
        Board.prototype.onEnterTransitionDidFinish.call(this);
        this.scheduleUpdate();
    },
    onExit: function () {
        Board.prototype.onExit.call(this);
        this.unscheduleUpdate();
    },

    /**
     * 自动加入房间
     * @private
     */
    update: function (dt) {
        Board.prototype.update.call(this, dt);
        var returnId = MWUtil.getReturnId();
        if (!isNullString(returnId)) {
            MWUtil.clearReturnId();
            returnId = parseInt(returnId);
            if (returnId != 0 && returnId.toString().length < 6) {
                this._inviteJoinClub(returnId);
            } else if (returnId.toString().length == 6) {
                showLoading('正在自动加入房间!');
                network.send(MsgCode.JoinRoom, {room_id: returnId});
            }
        }
        if (XianLiaoUtil.getReturnId) {
            returnId = XianLiaoUtil.getReturnId();
            if (!isNullString(returnId)) {
                returnId = parseInt(returnId);
                XianLiaoUtil.clearReturnId();
                if (returnId != 0 && returnId.toString().length < 6) {
                    this._inviteJoinClub(returnId);
                } else if (returnId.toString().length == 6) {
                    showLoading('正在自动加入房间!');
                    network.send(MsgCode.JoinRoom, {room_id: returnId});
                }
            }
        }
    },

    _initNetworkListeners: function () {
        Board.prototype._initNetworkListeners.call(this);
        this._networkListeners = {
            CreateRoom: '_onCreateRoom',
            JoinRoom: '_onJoinRoom',
            GS_UserJoin: '_onUserJoin',
            GS_UserJoin_NiuNiu: '_onUserJoin',
        };
    },

    /**
     * 创建房间
     * @param data  数据
     * @param errorCode  错误码
     * @private
     */
    _onCreateRoom: function (data, errorCode) {
        ClubMsgManager.isCreate = true ;
        var that = this;
        if (errorCode) {
            var errorMsg = '创建房间失败, 请重试';
            if (errorCode == -40) errorMsg = '创建房间失败, 您的房卡不足';
            if (errorCode == -2) errorMsg = '版本过低。请退出后重新登陆';
            if (data && data.errorMsg) {
                errorMsg = data.errorMsg;
            }
            showAlert(errorMsg);
            hideLoading();
            return;
        }

        //俱乐部创建的
        if (data && data['club_id']) {
            network.send(3002, {
                room_id: '' + data.room_id
            });
            return;
        }
        hideLoading();
        if (data && data['is_daikai']) {
            that.mapId = data['map_id'] || 0;
        }
    },

    /**
     * 加入房间
     * @param data
     * @param errorCode
     * @private
     */
    _onJoinRoom: function (data, errorCode) {
        if (errorCode) {
            var errorMsg = null;
            if (errorCode == -20) errorMsg = '房间号不存在, 请重新输入';
            if (errorCode == -30) errorMsg = '该房间已满员, 无法加入';
            if (errorCode == -60) errorMsg = '该房间已开始, 无法加入';
            if (errorCode == -40) errorMsg = '您的房卡不足';
            if (errorCode == -2) errorMsg = '版本过低。请退出后重新登陆';
            if (data && data.errorMsg) {
                errorMsg = data.errorMsg;
            } else if (errorCode == -1) {
                errorMsg = data.msg;
            } else if (data && data.msg) {
                errorMsg = data.msg;
            }
            hideLoading();
            if (errorCode == -93 || errorCode == -92) {
                gameData.players = data['players'];
                gameData.roomId = data['room_id'];
                gameData.distance = data['distance'] || 100;
                showWindow('PlayerInfoLocationWindow', true);
                return;
            }
            showAlert(errorMsg);
            return;
        }
        network.stop();
        ClubMsgManager.isCreate = false ;
        this._initRoom(data, ROOM_DATA_TYPE.CREATE);
    },

    /**
     * 胡子加入房间
     * @param data
     * @private
     */
    _onUserJoin: function (data) {
        var Result = null;
        if (data.Head) {
            Result = data.Head.Result;
        }
        if (data.Result != null) {
            Result = data.Result;
        }
        if (Result == 0) {
            SubUpdateUtil.showGameScene(data, ROOM_DATA_TYPE.CREATE);
        } else {
            hideLoading();
            if (data.Head) {
                showMessageBox(data.Head.ErrorMsg);
            } else {
                showMessageBox(data.ErrorMsg);
            }
        }
    },
    /**
     * 进入房间
     * @param data
     * @param roomDataType 是否是重连或回放
     * @private
     */
    _initRoom: function (data, roomDataType) {
        SubUpdateUtil.showGameScene(data, roomDataType);
    },

    /**
     * 邀请加入俱乐部
     * @param joinClubId
     * @private
     */
    _inviteJoinClub: function (joinClubId) {
        if (ClubDataManager.getClubData(joinClubId)) {
            ClubViewUtil.showClubMainWindow(joinClubId);
        } else {
            ClubViewUtil.showClubMainWindow();
            showWindow('ClubJoinOrInviteWindow', ClubJoinOrInviteWindow_Type.Join, joinClubId, true);
        }
    },

});